<?php

/**
 * This is the model class for table "{{category}}".
 *
 * The followings are the available columns in table '{{category}}':
 * @property integer $id
 * @property string $catename
 * @property integer $parentid
 * @property string $pnode
 * @property string $cnode
 */
class Category extends CDbActiveRecord {
	protected $cacheKey = 'yii::cache.{{db}}.{{table}}';
	
	// protected $cacheId = 'memcache';
	private $_enableCaching = true;
	
	private $_caching = array (
			'cacheId' => 'memcache',
			'class' => 'system.caching.CMemCache',
			'expire' => 6400 
	);
	
	public $alias = 'ca';
	
	public $criteria;
	
	public function __construct() {
		if ($this->_enableCaching) {
			$cache = new CActiveRecordCache ( $this->_caching );
		}
		if (!$this->criteria instanceof CDbCriteria) {
			$this->criteria = new CDbCriteria ();
		}
	}
	public function getCriteria() {
		return $this->criteria;
	}
	public function setCacheRule() {
	}
	// private $hidden = 2;
	// public $name = array (
	// 'name' => 'name1111','value' => 'value1111'
	// );
	public static function model($className = __CLASS__) {
		return parent::model ( $className );
	}
	
	public function tableName() {
		return 'webapp.wdb_category';
	}
	
	public function rules() {
		return array (
				array (
						'parentid',
						'numerical',
						'integerOnly' => true 
				),
				array (
						'catename',
						'length',
						'max' => 20 
				),
				array (
						'pnode, cnode',
						'length',
						'max' => 50 
				),
				array (
						'id, catename, parentid, pnode, cnode',
						'safe',
						'on' => 'search' 
				) 
		);
	}
	public function relations() {
		return array ();
	}
	public function attributeLabels() {
		return array (
				'id' => 'ID',
				'catename' => 'Catename',
				'parentid' => 'Parentid',
				'pnode' => 'Pnode',
				'cnode' => 'Cnode' 
		);
	}
	public function defaultSelectColumn() {
		return;
	}
	/*
	 * public function scopes() { return array ( 'enable' => array ( 'condtion'
	 * => 111 ) ); }
	 */
	public function search() {
		$criteria = new CDbCriteria ();
		$criteria->select = array (
				't.*' 
		);
		$criteria->compare ( 'id', $this->id );
		$criteria->compare ( 'catename', $this->catename, true );
		$criteria->compare ( 'parentid', $this->parentid );
		$criteria->compare ( 'pnode', $this->pnode, true );
		$criteria->compare ( 'cnode', $this->cnode, true );
		
		return new CActiveDataProvider ( $this, array (
				'criteria' => $criteria 
		) );
	} /*
	   * public function __set($name, $value) { $columns = $this->getMetaData
	   * ()->tableSchema; // echo CVarDumper::dumpAsString ( $columns,
	   * 10000000000000, true ); // $keys = array_keys($this); $a = array (
	   * 'name' => $name,'value' => $value ); // echo CVarDumper::dumpAsString (
	   * $this, 10000, true ); // parent::__set($name, $value); }
	   */
	/*public function __get($name)
	{
		//echo '<br>__get($name):$name===' . $name;
		//print_r ( array_keys ( $this->getMetaData ()->columns ) );
		//print_r ( $this->getMetaData ()->attributeDefaults );
		if ($bool = ! array_key_exists ( $name, $this->getMetaData ()->columns ))
		{
			// return $this->data[$name];
			// echo '<br>array_key_exists($name):';
			// var_dump ( $bool );
			// print_r ( $this->hidden );
			// var_dump ( property_exists ( $this, $name ) );
			if (! property_exists ( $this, $name ) && ! method_exists ( $this, $name ))
			{
				$getter = 'get' . $name;
			}
		} else
		{
			// echo 'dddddddd';
		}
		
		// var_dump(property_exists($this, $name));
		
		// return parent::__get ( $name );
	}*//*
	public function __sleep()
	{
		echo '<BR>';
		$a = array (
				'server','username','password','db' 
		);
		print_r ( $a );
		return $a;
	}
	public function __wakeup()
	{
		$this->getABC ();
	}
	public function getName()
	{
		/*
		 * return array ( 'name' => 'name1111','value' => 'value1111' );
		 /
	}*/
				public function getABC() {
		echo 'getABC';
	}
	
	/**
	 * 非魔术方法
	 */
	public function getHidden() {
		// echo '<BR>调用前';
		/*
		 * $a = $this->hidden = array ( 'x' );
		 */
		// echo '<BR>调用后';
		// var_dump ( $a );
		// return $this->hidden;
	}
	
	/*
	 * public function behaviors(){ return array(); }
	 */
	
	// 其他代码
	
	/**
	 * @inheritdoc
	 */
	public function beforeSave() {
		if (empty ( $this->parent_id ))
			$this->parent_id = null;
		return parent::beforeSave ();
		// 自己的行为事件
	}
	/**
	 * @inheritdoc
	 */
	protected function afterSave() {
		parent::afterSave ();
		
		// $cmd = $this->getDbConnection ()->createCommand ( '' );
		
		// $cmd->execute ();
	}
	/**
	 * (non-PHPdoc)
	 *
	 * @see CActiveRecord::defaultScope()
	 */
	public function defaultScope() {		
		if ($this->getDefaultScopeDisabled () == true) {
			$scope = array();
		} else {
			/*$scope = array (
					'alias'=>$this->alias,
					'condition' => $this->alias . '.isactive=1'
			);*/
			$scope = parent::defaultScope();
		}
		return $scope;
	}
	
	/**
	 * 
	 * $this->alias = 'ca'时调用setAlias()方法
	 * 
	 * @return string
	 */
	public function setAlias(){
		return $this->alias;
	}
	
	/**
	 * (non-PHPdoc)
	 *
	 * @see CActiveRecord::resetScope()
	 */
	public function resetScope1() {
		if (null !== ($criteria = $this->getDbCriteria ( false ))) {
			foreach ( get_class_vars ( 'CDbCriteria' ) as $k => $v )
				$criteria->$k = $v;
		}
		return $this;
	}
	/**
	 * @inheritdoc
	 */
	public function scopes() {
		return array (
				'root' => array (
						'condition' => $this->getTableAlias ( false, false ) . '.parentid=0' 
				) 
		);
	}
	
	function enableScope($enable = true) {
		if ($enable)
			$this->getDbCriteria ()->addCondition ( 'enable' );
		else
			$this->getDbCriteria ()->addCondition ( 'NOT enable' );		
		return $this;
	}
	
	/**
	 *
	 * @return Category
	 */
	function limit($count) {
		$this->getDbCriteria ()->mergeWith ( array (
				'limit' => $count 
		) );		
		return $this;
	}
	
	function suggest($keyword)
	{
		if (empty($keyword)) {
			return new CArrayDataProvider(array());
		}
		$criteria = new CDbCriteria();
		$criteria->compare('name', $keyword, true);
		$criteria->mergeWith($this->getDbCriteria());	
		return new CActiveDataProvider(self::model(), array(
				'criteria'=>$criteria,
		));
	}
	
	function getChildren($count = 1000) {
		$criteria = new CDbCriteria ();
		if (empty ( $this->id ))
			$criteria->addCondition ( 'parentid IS NULL' );
		else
			$criteria->compare ( 'parentid', $this->id );
		$criteria->order = '`index`';
		return new CActiveDataProvider ( $this, array (
				'criteria' => $criteria,
				'pagination' => array (
						'pageSize' => $count 
				) 
		) );
	}
}